{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "from py2neo import Graph, Node\n",
    "\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 导入CSV文件"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [],
   "source": [
    "df = pd.read_csv('medical_data2.csv', encoding='gbk')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 症状实体"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "symptoms = []\n",
    "for each in df['症状']:\n",
    "    symptoms.extend(each.split(','))\n",
    "symptoms = set(symptoms)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 疾病字典"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "disease_infos = [] # 疾病信息\n",
    "for idx, row in df.iterrows():\n",
    "    disease_infos.append(dict(row))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 关系函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [],
   "source": [
    "def deduplicate(rels_old):\n",
    "    '''关系去重函数'''\n",
    "    rels_new = []\n",
    "    for each in rels_old:\n",
    "        if each not in rels_new:\n",
    "            rels_new.append(each)\n",
    "    return rels_new"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 疾病-症状实体对"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "rels_symptom = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['症状'].split(','):\n",
    "        rels_symptom.append([row['疾病名称'], each])\n",
    "rels_symptom = deduplicate(rels_symptom)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 图数据库连接"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "g = Graph('http://localhost:7474', auth=('neo4j', '123456'))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 创建疾病实体"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建疾病实体： 肺泡蛋白质沉积症\n",
      "创建疾病实体： 百日咳\n",
      "创建疾病实体： 苯中毒\n",
      "创建疾病实体： 喘息样支气管炎\n",
      "创建疾病实体： 成人呼吸窘迫综合征\n",
      "创建疾病实体： 大量羊水吸入\n",
      "创建疾病实体： 单纯性肺嗜酸粒细胞浸润症\n",
      "创建疾病实体： 大叶性肺炎\n",
      "创建疾病实体： 大楼病综合征\n",
      "创建疾病实体： 二硫化碳中毒\n",
      "创建疾病实体： 肺-胸膜阿米巴病\n",
      "创建疾病实体： 肺出血－肾炎综合征\n",
      "创建疾病实体： 肺放线菌病\n",
      "创建疾病实体： 肺泡蛋白沉着症\n",
      "创建疾病实体： 肺曲菌病\n",
      "创建疾病实体： 放射性肺炎\n",
      "创建疾病实体： 肺念珠菌病\n",
      "创建疾病实体： 肺大疱\n",
      "创建疾病实体： 肺炎球菌肺炎\n",
      "创建疾病实体： 肺气肿\n",
      "创建疾病实体： 肺炎杆菌肺炎\n",
      "共创建 21 个疾病实体\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "for disease_dict in disease_infos:\n",
    "    try:\n",
    "        node = Node(\"Disease\",\n",
    "                    name=disease_dict['疾病名称'],\n",
    "                    # desc=disease_dict['疾病描述'],\n",
    "                    # prevent=disease_dict['预防措施'],\n",
    "                    cause=disease_dict['病因'],\n",
    "                    # easy_get=disease_dict['易感人群'],\n",
    "                    # cure_lasttime=disease_dict['疗程'],\n",
    "                    # cure_department=disease_dict['科室'],\n",
    "                    # cure_way=disease_dict['疗法'],\n",
    "                    # cured_prob=disease_dict['治愈率']\n",
    "                    )\n",
    "        g.create(node)\n",
    "        count += 1\n",
    "        print('创建疾病实体：', disease_dict['疾病名称'])\n",
    "    except:\n",
    "        pass\n",
    "print('共创建 {} 个疾病实体'.format(count))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 创建症状实体"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建实体 呕吐\n",
      "创建实体 抽搐\n",
      "创建实体 谵妄\n",
      "创建实体 变应性咳嗽\n",
      "创建实体 蛋白尿\n",
      "创建实体 咳铁锈色痰\n",
      "创建实体 腱反射消失\n",
      "创建实体 发热伴咳嗽、咯...\n",
      "创建实体 胸闷\n",
      "创建实体 盗汗\n",
      "创建实体 闫铁\n",
      "创建实体 纵隔浊音界扩大\n",
      "创建实体 恶心\n",
      "创建实体 心源性呼吸窘迫\n",
      "创建实体 目赤\n",
      "创建实体 嗜酸性粒细胞增多\n",
      "创建实体 浅感觉减退或缺失\n",
      "创建实体 咳嗽伴胸痛\n",
      "创建实体 哮鸣音\n",
      "创建实体 耸肩喘息\n",
      "创建实体 骨膜炎\n",
      "创建实体 面色青紫\n",
      "创建实体 头晕\n",
      "创建实体 多发性神经炎\n",
      "创建实体 痰中带血丝\n",
      "创建实体 呼吸困难\n",
      "创建实体 化学性支气管炎\n",
      "创建实体 喘息\n",
      "创建实体 口唇和甲床略带青紫\n",
      "创建实体 呼吸音减弱\n",
      "创建实体 痉挛性咳嗽\n",
      "创建实体 胸痛\n",
      "创建实体 惊厥\n",
      "创建实体 畏寒\n",
      "创建实体 气短\n",
      "创建实体 昏迷\n",
      "创建实体 小支气管粘膜水肿\n",
      "创建实体 胸闷憋气\n",
      "创建实体 眼睛痒\n",
      "创建实体 黏稠或脓性痰伴...\n",
      "创建实体 痰呈粘液脓性\n",
      "创建实体 毓卓\n",
      "创建实体 肺泡炎症\n",
      "创建实体 肺阴虚\n",
      "创建实体 紧张性头痛\n",
      "创建实体 肺部啰音\n",
      "创建实体 咳嗽伴哮鸣音\n",
      "创建实体 肺部出血\n",
      "创建实体 咽部异物感\n",
      "创建实体 乏力\n",
      "创建实体 咳出棕色痰栓\n",
      "创建实体 肺纹理增粗\n",
      "创建实体 口唇青紫\n",
      "创建实体 横膈低平\n",
      "创建实体 间歇性头痛\n",
      "创建实体 闫鹏辉\n",
      "创建实体 肺纤维化\n",
      "创建实体 呼气音延长\n",
      "创建实体 紧张性头晕\n",
      "创建实体 腹泻\n",
      "创建实体 气急\n",
      "创建实体 鼻塞\n",
      "创建实体 感觉障碍\n",
      "创建实体 低热\n",
      "创建实体 体重减轻\n",
      "创建实体 纤毛上皮细胞损伤脱落\n",
      "创建实体 肺泡灌洗液可见...\n",
      "创建实体 湿啰音\n",
      "创建实体 发烧\n",
      "创建实体 间歇性头晕\n",
      "创建实体 咯血\n",
      "创建实体 急性面容\n",
      "创建实体 吸气时有蝉鸣音\n",
      "创建实体 紫绀\n",
      "创建实体 冬春季的慢性咳...\n",
      "创建实体 出生后即有持续青紫\n",
      "创建实体 桶状胸\n",
      "创建实体 咯血伴发热\n"
     ]
    }
   ],
   "source": [
    "for each in symptoms:\n",
    "    node = Node('Symptom', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 关系函数创建"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [],
   "source": [
    "def create_relationship(start_node, end_node, edges, rel_type, rel_name):\n",
    "    '''创建关系函数'''\n",
    "    for edge in edges:\n",
    "        p = edge[0]\n",
    "        q = edge[1]\n",
    "        # 创建关系的 Cypher 语句\n",
    "        query = \"match(p:%s),(q:%s) where p.name='%s' and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)\" % (start_node, end_node, p, q, rel_type, rel_name)\n",
    "        try:\n",
    "            g.run(query) # 运行 Cypher 语句\n",
    "            print('创建关系 {}-{}->{}'.format(p, rel_type, q))\n",
    "        except Exception as e:\n",
    "            print(e)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 创建关系：疾病和症状"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建关系 肺泡蛋白质沉积症-has_symptom->紫绀\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->胸痛\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->呼吸困难\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->乏力\n",
      "创建关系 肺泡蛋白质沉积症-has_symptom->毓卓\n",
      "创建关系 百日咳-has_symptom->吸气时有蝉鸣音\n",
      "创建关系 百日咳-has_symptom->痉挛性咳嗽\n",
      "创建关系 百日咳-has_symptom->胸闷\n",
      "创建关系 百日咳-has_symptom->肺阴虚\n",
      "创建关系 百日咳-has_symptom->抽搐\n",
      "创建关系 百日咳-has_symptom->低热\n",
      "创建关系 百日咳-has_symptom->闫鹏辉\n",
      "创建关系 百日咳-has_symptom->惊厥\n",
      "创建关系 苯中毒-has_symptom->恶心\n",
      "创建关系 苯中毒-has_symptom->抽搐\n",
      "创建关系 苯中毒-has_symptom->感觉障碍\n",
      "创建关系 喘息样支气管炎-has_symptom->耸肩喘息\n",
      "创建关系 喘息样支气管炎-has_symptom->哮鸣音\n",
      "创建关系 喘息样支气管炎-has_symptom->纤毛上皮细胞损伤脱落\n",
      "创建关系 喘息样支气管炎-has_symptom->变应性咳嗽\n",
      "创建关系 喘息样支气管炎-has_symptom->化学性支气管炎\n",
      "创建关系 喘息样支气管炎-has_symptom->喘息\n",
      "创建关系 喘息样支气管炎-has_symptom->冬春季的慢性咳...\n",
      "创建关系 喘息样支气管炎-has_symptom->咳嗽伴哮鸣音\n",
      "创建关系 成人呼吸窘迫综合征-has_symptom->呼吸困难\n",
      "创建关系 成人呼吸窘迫综合征-has_symptom->紫绀\n",
      "创建关系 成人呼吸窘迫综合征-has_symptom->心源性呼吸窘迫\n",
      "创建关系 大量羊水吸入-has_symptom->面色青紫\n",
      "创建关系 大量羊水吸入-has_symptom->呼吸困难\n",
      "创建关系 大量羊水吸入-has_symptom->口唇青紫\n",
      "创建关系 大量羊水吸入-has_symptom->肺纹理增粗\n",
      "创建关系 大量羊水吸入-has_symptom->出生后即有持续青紫\n",
      "创建关系 大量羊水吸入-has_symptom->口唇和甲床略带青紫\n",
      "创建关系 大量羊水吸入-has_symptom->气急\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->嗜酸性粒细胞增多\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->咽部异物感\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->胸闷憋气\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->咯血伴发热\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->胸闷\n",
      "创建关系 单纯性肺嗜酸粒细胞浸润症-has_symptom->乏力\n",
      "创建关系 大叶性肺炎-has_symptom->湿啰音\n",
      "创建关系 大叶性肺炎-has_symptom->胸痛\n",
      "创建关系 大叶性肺炎-has_symptom->发烧\n",
      "创建关系 大叶性肺炎-has_symptom->咳铁锈色痰\n",
      "创建关系 大叶性肺炎-has_symptom->急性面容\n",
      "创建关系 大叶性肺炎-has_symptom->呼吸音减弱\n",
      "创建关系 大楼病综合征-has_symptom->紧张性头晕\n",
      "创建关系 大楼病综合征-has_symptom->紧张性头痛\n",
      "创建关系 大楼病综合征-has_symptom->间歇性头晕\n",
      "创建关系 大楼病综合征-has_symptom->鼻塞\n",
      "创建关系 大楼病综合征-has_symptom->头晕\n",
      "创建关系 大楼病综合征-has_symptom->恶心\n",
      "创建关系 大楼病综合征-has_symptom->眼睛痒\n",
      "创建关系 大楼病综合征-has_symptom->目赤\n",
      "创建关系 大楼病综合征-has_symptom->间歇性头痛\n",
      "创建关系 二硫化碳中毒-has_symptom->昏迷\n",
      "创建关系 二硫化碳中毒-has_symptom->腱反射消失\n",
      "创建关系 二硫化碳中毒-has_symptom->呕吐\n",
      "创建关系 二硫化碳中毒-has_symptom->谵妄\n",
      "创建关系 二硫化碳中毒-has_symptom->多发性神经炎\n",
      "创建关系 二硫化碳中毒-has_symptom->浅感觉减退或缺失\n",
      "创建关系 二硫化碳中毒-has_symptom->恶心\n",
      "创建关系 二硫化碳中毒-has_symptom->感觉障碍\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->盗汗\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->腹泻\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->乏力\n",
      "创建关系 肺-胸膜阿米巴病-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->肺部出血\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->呼吸困难\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->咯血\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->气短\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺出血－肾炎综合征-has_symptom->蛋白尿\n",
      "创建关系 肺放线菌病-has_symptom->骨膜炎\n",
      "创建关系 肺放线菌病-has_symptom->体重减轻\n",
      "创建关系 肺放线菌病-has_symptom->咯血\n",
      "创建关系 肺放线菌病-has_symptom->盗汗\n",
      "创建关系 肺放线菌病-has_symptom->乏力\n",
      "创建关系 肺放线菌病-has_symptom->低热\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->胸痛\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->紫绀\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->呼吸困难\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->肺泡炎症\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->肺泡灌洗液可见...\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->乏力\n",
      "创建关系 肺泡蛋白沉着症-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺曲菌病-has_symptom->咳出棕色痰栓\n",
      "创建关系 肺曲菌病-has_symptom->嗜酸性粒细胞增多\n",
      "创建关系 肺曲菌病-has_symptom->胸痛\n",
      "创建关系 放射性肺炎-has_symptom->闫铁\n",
      "创建关系 放射性肺炎-has_symptom->肺纤维化\n",
      "创建关系 放射性肺炎-has_symptom->胸痛\n",
      "创建关系 放射性肺炎-has_symptom->低热\n",
      "创建关系 肺念珠菌病-has_symptom->痰呈粘液脓性\n",
      "创建关系 肺念珠菌病-has_symptom->肺部啰音\n",
      "创建关系 肺大疱-has_symptom->胸闷\n",
      "创建关系 肺大疱-has_symptom->气短\n",
      "创建关系 肺大疱-has_symptom->胸闷憋气\n",
      "创建关系 肺大疱-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺炎球菌肺炎-has_symptom->痰中带血丝\n",
      "创建关系 肺炎球菌肺炎-has_symptom->咳嗽伴胸痛\n",
      "创建关系 肺炎球菌肺炎-has_symptom->胸痛\n",
      "创建关系 肺炎球菌肺炎-has_symptom->发热伴咳嗽、咯...\n",
      "创建关系 肺气肿-has_symptom->纵隔浊音界扩大\n",
      "创建关系 肺气肿-has_symptom->桶状胸\n",
      "创建关系 肺气肿-has_symptom->小支气管粘膜水肿\n",
      "创建关系 肺气肿-has_symptom->黏稠或脓性痰伴...\n",
      "创建关系 肺气肿-has_symptom->横膈低平\n",
      "创建关系 肺气肿-has_symptom->胸闷\n",
      "创建关系 肺气肿-has_symptom->呼气音延长\n",
      "创建关系 肺气肿-has_symptom->肺部啰音\n",
      "创建关系 肺气肿-has_symptom->呼吸音减弱\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->畏寒\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->胸痛\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->气急\n",
      "创建关系 肺炎杆菌肺炎-has_symptom->发热伴咳嗽、咯...\n"
     ]
    }
   ],
   "source": [
    "create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', '症状')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 删除所有结点"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "(No data)",
      "text/html": "(No data)"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cypher = 'MATCH (n) DETACH DELETE n'\n",
    "g.run(cypher)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
